//httpRequest => axios实例；interceptors => 分离出来的拦截器；option => 闭包参数
export function loadInterceptors(httpRequest, interceptors, option) {
  const {request, response} = interceptors
  // 加载请求拦截器
  request.forEach(item => {
    let {onFulfilled, onRejected} = item
    if (!onFulfilled || typeof onFulfilled !== 'function') {
      onFulfilled = config => config
    }
    if (!onRejected || typeof onRejected !== 'function') {
      onRejected = error => Promise.reject(error)
    }
    httpRequest.interceptors.request.use(
      //生成闭包!!!这样在拦截器调用的时候能够有option参数
      config => onFulfilled(config, option),
      config => onRejected(config, option)
    )
  })
  // 加载响应拦截器
  response.forEach(item => {
    let {onFulfilled, onRejected} = item
    if (!onFulfilled || typeof onFulfilled !== 'function') {
      onFulfilled = response => response
    }
    if (!onRejected || typeof onRejected !== 'function') {
      onRejected = error => Promise.reject(error)
    }
    httpRequest.interceptors.request.use(
      config => onFulfilled(config, option),
      config => onRejected(config, option)
    )
  })
}
